home *** CD-ROM | disk | FTP | other *** search
/ Champak 141 / (Vol 141) Oct 17 2011.iso / Games / Clueless.swf / scripts / Common / SoundManager.as < prev    next >
Encoding:
Text File  |  2011-10-17  |  4.3 KB  |  172 lines

  1. package Common
  2. {
  3.    import flash.media.Sound;
  4.    import flash.media.SoundChannel;
  5.    import flash.media.SoundMixer;
  6.    import flash.media.SoundTransform;
  7.    
  8.    public class SoundManager
  9.    {
  10.       
  11.       private static var instance:SoundManager;
  12.        
  13.       
  14.       private var sounds:Array;
  15.       
  16.       private var sfxVolume:Number = 0.1;
  17.       
  18.       private var bgPlayingName:String = "";
  19.       
  20.       private var bgVolume:Number = 0.1;
  21.       
  22.       private var bgSoundChannel:SoundChannel;
  23.       
  24.       private var isMute:Boolean = false;
  25.       
  26.       private var isBGPlaying:Boolean = false;
  27.       
  28.       public function SoundManager()
  29.       {
  30.          sounds = [];
  31.          sfxVolume = 0.1;
  32.          bgVolume = 0.1;
  33.          isMute = false;
  34.          bgPlayingName = "";
  35.          isBGPlaying = false;
  36.          super();
  37.       }
  38.       
  39.       public static function getInstance() : SoundManager
  40.       {
  41.          if(instance == null)
  42.          {
  43.             instance = new SoundManager();
  44.          }
  45.          return instance;
  46.       }
  47.       
  48.       public function toggleMuteSound() : *
  49.       {
  50.          SoundMixer.stopAll();
  51.          isMute = !isMute;
  52.       }
  53.       
  54.       public function get BGVolume() : Number
  55.       {
  56.          return bgVolume;
  57.       }
  58.       
  59.       public function playSoundFromRes(param1:String, param2:Sound, param3:uint) : *
  60.       {
  61.          var _loc4_:SoundTransform = null;
  62.          if(isMute)
  63.          {
  64.             return;
  65.          }
  66.          _loc4_ = new SoundTransform();
  67.          if(param3 == SoundObject.C_BG)
  68.          {
  69.             if(isBGPlaying && param1 == bgPlayingName)
  70.             {
  71.                return;
  72.             }
  73.             stopBG();
  74.             _loc4_.volume = bgVolume;
  75.             bgSoundChannel = param2.play(0,int.MAX_VALUE,_loc4_);
  76.             bgPlayingName = param1;
  77.             isBGPlaying = true;
  78.          }
  79.          else if(param3 == SoundObject.C_SFX)
  80.          {
  81.             _loc4_.volume = sfxVolume;
  82.             param2.play(0,0,_loc4_);
  83.          }
  84.       }
  85.       
  86.       public function get SFXVolume() : Number
  87.       {
  88.          return sfxVolume;
  89.       }
  90.       
  91.       public function set BGVolume(param1:Number) : *
  92.       {
  93.          bgVolume = param1;
  94.          bgSoundChannel.soundTransform = new SoundTransform(param1);
  95.       }
  96.       
  97.       public function purgeSound(param1:String) : *
  98.       {
  99.          sounds[param1] = null;
  100.       }
  101.       
  102.       public function stopBG() : *
  103.       {
  104.          if(bgSoundChannel != null)
  105.          {
  106.             bgSoundChannel.stop();
  107.             isBGPlaying = false;
  108.          }
  109.       }
  110.       
  111.       public function addSound(param1:String, param2:Sound, param3:uint, param4:uint) : *
  112.       {
  113.          sounds[param1] = new SoundObject(param1,param2,param3,param4);
  114.       }
  115.       
  116.       public function purgeAll() : *
  117.       {
  118.          var _loc1_:String = null;
  119.          for(_loc1_ in sounds)
  120.          {
  121.             purgeSound(_loc1_);
  122.          }
  123.       }
  124.       
  125.       public function playSound(param1:String) : *
  126.       {
  127.          var _loc2_:SoundChannel = null;
  128.          var _loc3_:SoundObject = null;
  129.          var _loc4_:SoundTransform = null;
  130.          _loc2_ = null;
  131.          if(isMute)
  132.          {
  133.             return _loc2_;
  134.          }
  135.          _loc3_ = sounds[param1];
  136.          _loc4_ = new SoundTransform();
  137.          if(_loc3_.Position == SoundObject.C_RIGHT)
  138.          {
  139.             _loc4_.pan = 1;
  140.          }
  141.          else if(_loc3_.Position == SoundObject.C_LEFT)
  142.          {
  143.             _loc4_.pan = -1;
  144.          }
  145.          else
  146.          {
  147.             _loc4_.pan = 0;
  148.          }
  149.          if(_loc3_.Type == SoundObject.C_BG && isBGPlaying && param1 != bgPlayingName)
  150.          {
  151.             stopBG();
  152.             _loc4_.volume = bgVolume;
  153.             bgSoundChannel = _loc3_.SoundData.play(0,int.MAX_VALUE,_loc4_);
  154.             bgPlayingName = param1;
  155.             isBGPlaying = true;
  156.             _loc2_ = bgSoundChannel;
  157.          }
  158.          else if(_loc3_.Type == SoundObject.C_SFX)
  159.          {
  160.             _loc4_.volume = sfxVolume;
  161.             _loc2_ = _loc3_.SoundData.play(0,0,_loc4_);
  162.          }
  163.          return _loc2_;
  164.       }
  165.       
  166.       public function set SFXVolume(param1:Number) : *
  167.       {
  168.          sfxVolume = param1;
  169.       }
  170.    }
  171. }
  172.